<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      7-包  | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>7-包 </h2>



  <p class="post-date">2019-03-10</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h3 id="什么是包，为什么使用包？"><a href="#什么是包，为什么使用包？" class="headerlink" title="什么是包，为什么使用包？"></a>什么是包，为什么使用包？</h3><p>到目前为止，我们看到的 Go 程序都只有一个文件，文件里包含一个 main 函数和几个其他的函数。在实际中，这种把所有源代码编写在一个文件的方法并不好用。以这种方式编写，代码的重用和维护都会很困难。而包（Package）解决了这样的问题。</p>
<p><strong>包用于组织 Go 源代码，提供了更好的可重用性与可读性</strong>。由于包提供了代码的封装，因此使得 Go 应用程序易于维护。</p>
<p>例如，假如我们正在开发一个 Go  图像处理程序，它提供了图像的裁剪、锐化、模糊和彩色增强等功能。一种组织程序的方式就是根据不同的特性，把代码放到不同的包中。比如裁剪可以是一个单独的包，而锐化是另一个包。这种方式的优点是，由于彩色增强可能需要一些锐化的功能，因此彩色增强的代码只需要简单地导入（我们会在随后讨论）锐化功能的包，就可以使用锐化的功能了。这样的方式使得代码易于重用。</p>
<p>我们会逐步构建一个计算矩形的面积和对角线的应用程序。</p>
<p>通过这个程序，我们会更好地理解包。</p>
<h3 id="main-函数和-main-包"><a href="#main-函数和-main-包" class="headerlink" title="main 函数和 main 包"></a>main 函数和 main 包</h3><p>所有可执行的 Go 程序都必须包含一个 main 函数。这个函数是程序运行的入口。main 函数应该放置于 main 包中。</p>
<p><strong>package packagename 这行代码指定了某一源文件属于一个包。它应该放在每一个源文件的第一行。</strong></p>
<p>下面开始为我们的程序创建一个 main 函数和 main 包。<strong>在 Go 工作区内的 src 文件夹中创建一个文件夹，命名为 geometry</strong>。在 <code>geometry</code> 文件夹中创建一个 <code>geometry.go</code> 文件。</p>
<p>在 geometry.go 中编写下面代码。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// geometry.go</span></span><br><span class="line"><span class="keyword">package</span> main </span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Geometrical shape properties"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>package main</code> 这一行指定该文件属于 main 包。<code>import &quot;packagename&quot;</code> 语句用于导入一个已存在的包。在这里我们导入了 <code>fmt</code> 包，包内含有 Println 方法。接下来是 main 函数，它会打印 <code>Geometrical shape properties</code>。</p>
<p>键入 <code>go install geometry</code>，编译上述程序。该命令会在 <code>geometry</code> 文件夹内搜索拥有 main 函数的文件。在这里，它找到了 <code>geometry.go</code>。接下来，它编译并产生一个名为 <code>geometry</code> （在 windows 下是 <code>geometry.exe</code>）的二进制文件，该二进制文件放置于工作区的 bin 文件夹。现在，工作区的目录结构会是这样：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">src</span><br><span class="line">    geometry</span><br><span class="line">        gemometry.go</span><br><span class="line">bin</span><br><span class="line">    geometry</span><br></pre></td></tr></table></figure>

<p>键入 <code>workspacepath/bin/geometry</code>，运行该程序。请用你自己的 Go 工作区来替换 <code>workspacepath</code>。这个命令会执行 bin 文件夹里的 <code>geometry</code> 二进制文件。你应该会输出 <code>Geometrical shape properties</code>。</p>
<h3 id="创建自定义的包"><a href="#创建自定义的包" class="headerlink" title="创建自定义的包"></a>创建自定义的包</h3><p>我们将组织代码，使得所有与矩形有关的功能都放入 <code>rectangle</code> 包中。</p>
<p>我们会创建一个自定义包 <code>rectangle</code>，它有一个计算矩形的面积和对角线的函数。</p>
<p><strong>属于某一个包的源文件都应该放置于一个单独命名的文件夹里。按照 Go 的惯例，应该用包名命名该文件夹。</strong></p>
<p>因此，我们在 <code>geometry</code> 文件夹中，创建一个命名为 <code>rectangle</code> 的文件夹。在 <code>rectangle</code> 文件夹中，所有文件都会以 <code>package rectangle</code> 作为开头，因为它们都属于 rectangle 包。</p>
<p>在我们之前创建的 rectangle 文件夹中，再创建一个名为 <code>rectprops.go</code> 的文件，添加下列代码。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// rectprops.go</span></span><br><span class="line"><span class="keyword">package</span> rectangle</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"math"</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Area</span><span class="params">(<span class="built_in">len</span>, wid <span class="keyword">float64</span>)</span> <span class="title">float64</span></span> &#123;  </span><br><span class="line">    area := <span class="built_in">len</span> * wid</span><br><span class="line">    <span class="keyword">return</span> area</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Diagonal</span><span class="params">(<span class="built_in">len</span>, wid <span class="keyword">float64</span>)</span> <span class="title">float64</span></span> &#123;  </span><br><span class="line">    diagonal := math.Sqrt((<span class="built_in">len</span> * <span class="built_in">len</span>) + (wid * wid))</span><br><span class="line">    <span class="keyword">return</span> diagonal</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在上面的代码中，我们创建了两个函数用于计算 <code>Area</code> 和 <code>Diagonal</code>。矩形的面积是长和宽的乘积。矩形的对角线是长与宽平方和的平方根。<code>math</code> 包下面的 <code>Sqrt</code> 函数用于计算平方根。</p>
<p>注意到函数 Area 和 Diagonal 都是以大写字母开头的。这是有必要的，我们将会很快解释为什么需要这样做。</p>
<h3 id="导入自定义包"><a href="#导入自定义包" class="headerlink" title="导入自定义包"></a>导入自定义包</h3><p>为了使用自定义包，我们必须要先导入它。导入自定义包的语法为 <code>import path</code>。我们必须指定自定义包相对于工作区内 <code>src</code> 文件夹的相对路径。我们目前的文件夹结构是：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">src</span><br><span class="line">    geometry</span><br><span class="line">        geometry.go</span><br><span class="line">        rectangle</span><br><span class="line">            rectprops.go</span><br></pre></td></tr></table></figure>

<p><code>import &quot;geometry/rectangle&quot;</code> 这一行会导入 rectangle 包。</p>
<p>在 <code>geometry.go</code> 里面添加下面的代码：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// geometry.go</span></span><br><span class="line"><span class="keyword">package</span> main </span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"geometry/rectangle"</span> <span class="comment">// 导入自定义包</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="keyword">var</span> rectLen, rectWidth <span class="keyword">float64</span> = <span class="number">6</span>, <span class="number">7</span></span><br><span class="line">    fmt.Println(<span class="string">"Geometrical shape properties"</span>)</span><br><span class="line">    <span class="comment">/*Area function of rectangle package used*/</span></span><br><span class="line">    fmt.Printf(<span class="string">"area of rectangle %.2f\n"</span>, rectangle.Area(rectLen, rectWidth))</span><br><span class="line">    <span class="comment">/*Diagonal function of rectangle package used*/</span></span><br><span class="line">    fmt.Printf(<span class="string">"diagonal of the rectangle %.2f "</span>, rectangle.Diagonal(rectLen, rectWidth))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的代码导入了 <code>rectangle</code> 包，并调用了里面的 Area 和 Diagonal 函数，得到矩形的面积和对角线。Printf 内的格式说明符 <code>%.2f</code> 会将浮点数截断到小数点两位。应用程序的输出为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Geometrical shape properties  </span><br><span class="line">area of rectangle 42.00  </span><br><span class="line">diagonal of the rectangle 9.22</span><br></pre></td></tr></table></figure>

<h3 id="导出名字（Exported-Names）"><a href="#导出名字（Exported-Names）" class="headerlink" title="导出名字（Exported Names）"></a>导出名字（Exported Names）</h3><p>我们将 rectangle 包中的函数 Area 和 Diagonal 首字母大写。在 Go 中这具有特殊意义。在 Go  中，任何以大写字母开头的变量或者函数都是被导出的名字。其它包只能访问被导出的函数和变量。在这里，我们需要在 main 包中访问 Area 和  Diagonal 函数，因此会将它们的首字母大写。</p>
<p>在 <code>rectprops.go</code> 中，如果函数名从 <code>Area(len, wid float64)</code> 变为 <code>area(len, wid float64)</code>，并且在 <code>geometry.go</code> 中， <code>rectangle.Area(rectLen, rectWidth)</code> 变为 <code>rectangle.area(rectLen, rectWidth)</code>， 则该程序运行时，编译器会抛出错误 <code>geometry.go:11: cannot refer to unexported name rectangle.area</code>。因为如果想在包外访问一个函数，它应该首字母大写。</p>
<h3 id="init-函数"><a href="#init-函数" class="headerlink" title="init 函数"></a>init 函数</h3><p>所有包都可以包含一个 <code>init</code> 函数。init 函数不应该有任何返回值类型和参数，在我们的代码中也不能显式地调用它。init 函数的形式如下：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">init</span><span class="params">()</span></span> &#123;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>init 函数可用于执行初始化任务，也可用于在开始执行之前验证程序的正确性。</p>
<p>包的初始化顺序如下：</p>
<ol>
<li>首先初始化包级别（Package Level）的变量</li>
<li>紧接着调用 init 函数。包可以有多个 init 函数（在一个文件或分布于多个文件中），它们按照编译器解析它们的顺序进行调用。</li>
</ol>
<p>如果一个包导入了另一个包，会先初始化被导入的包。</p>
<p>尽管一个包可能会被导入多次，但是它只会被初始化一次。</p>
<p>为了理解 init 函数，我们接下来对程序做了一些修改。</p>
<p>首先在 <code>rectprops.go</code> 文件中添加了一个 init 函数。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// rectprops.go</span></span><br><span class="line"><span class="keyword">package</span> rectangle</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"math"</span>  </span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * init function added</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">init</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"rectangle package initialized"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Area</span><span class="params">(<span class="built_in">len</span>, wid <span class="keyword">float64</span>)</span> <span class="title">float64</span></span> &#123;  </span><br><span class="line">    area := <span class="built_in">len</span> * wid</span><br><span class="line">    <span class="keyword">return</span> area</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Diagonal</span><span class="params">(<span class="built_in">len</span>, wid <span class="keyword">float64</span>)</span> <span class="title">float64</span></span> &#123;  </span><br><span class="line">    diagonal := math.Sqrt((<span class="built_in">len</span> * <span class="built_in">len</span>) + (wid * wid))</span><br><span class="line">    <span class="keyword">return</span> diagonal</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们添加了一个简单的 init 函数，它仅打印 <code>rectangle package initialized</code>。</p>
<p>现在我们来修改 main 包。我们知道矩形的长和宽都应该大于 0，我们将在 <code>geometry.go</code> 中使用 init 函数和包级别的变量来检查矩形的长和宽。</p>
<p>修改 <code>geometry.go</code> 文件如下所示：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// geometry.go</span></span><br><span class="line"><span class="keyword">package</span> main </span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"geometry/rectangle"</span> <span class="comment">// 导入自定义包</span></span><br><span class="line">    <span class="string">"log"</span></span><br><span class="line">)</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * 1. 包级别变量</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">var</span> rectLen, rectWidth <span class="keyword">float64</span> = <span class="number">6</span>, <span class="number">7</span> </span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*2. init 函数会检查长和宽是否大于0</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">init</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    <span class="built_in">println</span>(<span class="string">"main package initialized"</span>)</span><br><span class="line">    <span class="keyword">if</span> rectLen &lt; <span class="number">0</span> &#123;</span><br><span class="line">        log.Fatal(<span class="string">"length is less than zero"</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> rectWidth &lt; <span class="number">0</span> &#123;</span><br><span class="line">        log.Fatal(<span class="string">"width is less than zero"</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;  </span><br><span class="line">    fmt.Println(<span class="string">"Geometrical shape properties"</span>)</span><br><span class="line">    fmt.Printf(<span class="string">"area of rectangle %.2f\n"</span>, rectangle.Area(rectLen, rectWidth))</span><br><span class="line">    fmt.Printf(<span class="string">"diagonal of the rectangle %.2f "</span>,rectangle.Diagonal(rectLen, rectWidth))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们对 <code>geometry.go</code> 做了如下修改：</p>
<ol>
<li>变量 <strong>rectLen</strong> 和 <strong>rectWidth</strong> 从 main 函数级别移到了包级别。</li>
<li>添加了 init 函数。当 rectLen 或 rectWidth 小于 0 时，init 函数使用 <strong>log.Fatal</strong> 函数打印一条日志，并终止了程序。</li>
</ol>
<p>main 包的初始化顺序为：</p>
<ol>
<li>首先初始化被导入的包。因此，首先初始化了 rectangle 包。</li>
<li>接着初始化了包级别的变量 <strong>rectLen</strong> 和 <strong>rectWidth</strong>。</li>
<li>调用 init 函数。</li>
<li>最后调用 main 函数。</li>
</ol>
<p>当运行该程序时，会有如下输出。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">rectangle package initialized  </span><br><span class="line">main package initialized  </span><br><span class="line">Geometrical shape properties  </span><br><span class="line">area of rectangle 42.00  </span><br><span class="line">diagonal of the rectangle 9.22</span><br></pre></td></tr></table></figure>

<p>果然，程序会首先调用 rectangle 包的 init 函数，然后，会初始化包级别的变量 <strong>rectLen</strong> 和 <strong>rectWidth</strong>。接着调用 main 包里的 init 函数，该函数检查 rectLen 和 rectWidth 是否小于 0，如果条件为真，则终止程序。我们会在单独的教程里深入学习 if 语句。现在你可以认为 <code>if rectLen &lt; 0</code> 能够检查 <code>rectLen</code> 是否小于 0，并且如果是，则终止程序。<code>rectWidth</code> 条件的编写也是类似的。在这里两个条件都为假，因此程序继续执行。最后调用了 main 函数。</p>
<p>让我们接着稍微修改这个程序来学习使用 init 函数。</p>
<p>将 <code>geometry.go</code> 中的 <code>var rectLen, rectWidth float64 = 6, 7</code> 改为 <code>var rectLen, rectWidth float64 = -6, 7</code>。我们把 <code>rectLen</code> 初始化为负数。</p>
<p>现在当运行程序时，会得到：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">rectangle package initialized  </span><br><span class="line">main package initialized  </span><br><span class="line">2017&#x2F;04&#x2F;04 00:28:20 length is less than zero</span><br></pre></td></tr></table></figure>

<p>像往常一样， 会首先初始化 rectangle 包，然后是 main 包中的包级别的变量 rectLen 和 rectWidth。rectLen 为负数，因此当运行 init 函数时，程序在打印 <code>length is less than zero</code> 后终止。</p>
<h3 id="使用空白标识符（Blank-Identifier）"><a href="#使用空白标识符（Blank-Identifier）" class="headerlink" title="使用空白标识符（Blank Identifier）"></a>使用空白标识符（Blank Identifier）</h3><p>导入了包，却不在代码中使用它，这在 Go 中是非法的。当这么做时，编译器是会报错的。其原因是为了避免导入过多未使用的包，从而导致编译时间显著增加。将 <code>geometry.go</code> 中的代码替换为如下代码：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// geometry.go</span></span><br><span class="line"><span class="keyword">package</span> main </span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"geometry/rectangle"</span> <span class="comment">// 导入自定的包</span></span><br><span class="line">)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的程序将会抛出错误 <code>geometry.go:6: imported and not used: &quot;geometry/rectangle&quot;</code>。</p>
<p>然而，在程序开发的活跃阶段，又常常会先导入包，而暂不使用它。遇到这种情况就可以使用空白标识符 <code>_</code>。</p>
<p>下面的代码可以避免上述程序的错误：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (  </span><br><span class="line">    <span class="string">"geometry/rectangle"</span> </span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> _ = rectangle.Area <span class="comment">// 错误屏蔽器</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>var _ = rectangle.Area</code> 这一行屏蔽了错误。我们应该了解这些错误屏蔽器（Error Silencer）的动态，在程序开发结束时就移除它们，包括那些还没有使用过的包。由此建议在 import 语句下面的包级别范围中写上错误屏蔽器。</p>
<p>有时候我们导入一个包，只是为了确保它进行了初始化，而无需使用包中的任何函数或变量。例如，我们或许需要确保调用了 rectangle 包的 init 函数，而不需要在代码中使用它。这种情况也可以使用空白标识符，如下所示。</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main </span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    _ <span class="string">"geometry/rectangle"</span> </span><br><span class="line">)</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行上面的程序，会输出 <code>rectangle package initialized</code>。尽管在所有代码里，我们都没有使用这个包，但还是成功初始化了它。</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Go" >
    <span class="tag-code">Go</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/go/2-Go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/14-%E5%AD%97%E7%AC%A6%E4%B8%B2/">
        <span class="nav-arrow">← </span>
        
          14-字符串
        
      </a>
    
    
      <a class="nav-right" href="/go/2-Go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/04-%E7%B1%BB%E5%9E%8B/">
        
          4-类型
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#什么是包，为什么使用包？"><span class="toc-nav-text">什么是包，为什么使用包？</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#main-函数和-main-包"><span class="toc-nav-text">main 函数和 main 包</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#创建自定义的包"><span class="toc-nav-text">创建自定义的包</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#导入自定义包"><span class="toc-nav-text">导入自定义包</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#导出名字（Exported-Names）"><span class="toc-nav-text">导出名字（Exported Names）</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#init-函数"><span class="toc-nav-text">init 函数</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#使用空白标识符（Blank-Identifier）"><span class="toc-nav-text">使用空白标识符（Blank Identifier）</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/go/2-Go语言基础/07-包/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "7-包 ",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>